package com.example.joemi.carpool;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.baidu.mapapi.search.core.PoiInfo;

import java.util.List;

/**
 * Created by joemi on 2015/10/7.
 */
public class TerminusAdapter extends BaseAdapter {

    private List<PoiInfo> pois;
    private LayoutInflater inflater;

    private class ViewHolder {
        TextView name;
        TextView addr;
    }

    public TerminusAdapter(Context context, List<PoiInfo> objects) {
        this.pois = objects;
        inflater = LayoutInflater.from(context);
        if(objects.size() == 0) {
            Log.i("in TerminusAdapter", ":: no objects!!");
        }
    }

    public int getCount() {
        return pois.size();
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        PoiInfo poiInfo = pois.get(position);
        ViewHolder holder = null;

        if(convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.cell_item_pois, null);
            holder.name = (TextView)convertView.findViewById(R.id.text_name);
            holder.addr = (TextView)convertView.findViewById(R.id.text_addr);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        holder.name.setText(poiInfo.name);
        holder.addr.setText(poiInfo.address);
        return convertView;
    }

    public long getItemId(int position) {
        return position;
    }

    public Object getItem(int position) {
        return pois.get(position);
    }
}
